package com.ktjy.controller;

import com.ktjy.entity.Flower;
import com.ktjy.service.FlowerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class FlowerController {

    @Autowired
    FlowerService flowerService;

    @GetMapping("/")
    public String select(){
        return "select";
    }

    @GetMapping("/select")
    public String selectFlower(Model model) {
        List<Flower> list = flowerService.select();
        model.addAttribute("list", list);
        return "select";
    }

    @RequestMapping(value = "/add")
    public String add(){
        return "add";
    }

    @RequestMapping(value = "/save")
    public String save(Flower flower, Model model){
        int count = flowerService.insert(flower);
        if(count > 0){
            model.addAttribute("add",count);
            return "redirect:/select";
        }else {
            model.addAttribute("add",0);
        }
        return null;
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam("flowerId") Integer flowerId){
        flowerService.delete(flowerId);
        return "redirect:/select";
    }

}
